<template>
  <div>
    <!-- 搜索栏 -->
    <el-card id="search">
      <el-row>
        <el-col :span="20">
          <el-input
            v-model="searchModel.type"
            placeholder="活动类型名称"
          ></el-input>
          <el-button
            type="primary"
            round
            icon="el-icon-search"
            @click="getEventTypeList"
            >查询</el-button
          >
        </el-col>
        <el-col :span="4" align="right">
          <el-button
            @click="openEditUI(null)"
            type="primary"
            icon="el-icon-plus"
            circle
          ></el-button>
        </el-col>
      </el-row>
    </el-card>

    <!-- 结果列表 -->
    <el-card>
      <el-table :data="eventTypeList" stripe style="width: 100%">
        <el-table-column
          prop="id"
          label="ID"
          width="50"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="type"
          label="活动类型名称"
          width="150"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="description"
          label="活动类型简介"
          width="250"
          align="center"
        ></el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <div>
              <el-button
                @click="openEditUI(scope.row.id)"
                type="primary"
                icon="el-icon-edit"
                size="mini"
                circle
              ></el-button>
              <el-button
                @click="deleteEventType(scope.row)"
                type="danger"
                icon="el-icon-delete"
                size="mini"
                circle
              ></el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 分页组件 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="searchModel.pageNo"
      :page-sizes="[5, 10, 20, 50]"
      :page-size="searchModel.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>

    <!-- 活动类型编辑对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="dialogFormVisible"
      @close="clearForm"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
    >
      <el-form :model="eventTypeForm" ref="eventTypeFormRef" :rules="rules">
        <el-form :model="eventTypeForm" ref="eventTypeFormRef" :rules="rules">
          <!-- 活动类型名称 -->
          <el-form-item
            label="活动类型名称"
            prop="type"
            :label-width="formLabelWidth"
          >
            <el-input
              v-model="eventTypeForm.type"
              autocomplete="off"
            ></el-input>
          </el-form-item>

          <!-- 活动类型简介 -->
          <el-form-item
            label="活动类型简介"
            prop="description"
            :label-width="formLabelWidth"
          >
            <el-input
              v-model="eventTypeForm.description"
              autocomplete="off"
            ></el-input>
          </el-form-item>
        </el-form>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="saveEventType">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import eventTypeApi from "@/api/eventTypeManage"; // 替换为活动类型的 API 路径
export default {
  data() {
    return {
      formLabelWidth: "130px",
      eventTypeForm: {
        type: "",
        description: "",
      },
      dialogFormVisible: false,
      title: "",
      total: 0,
      searchModel: {
        pageNo: 1,
        pageSize: 10,
        type: "",
      },
      eventTypeList: [],
      rules: {
        type: [
          { required: true, message: "请输入活动类型名称", trigger: "blur" },
        ],
        description: [
          { required: true, message: "请输入活动类型简介", trigger: "blur" },
        ],
      },
    };
  },
  mounted() {
    this.getEventTypeList();
  },
  methods: {
    deleteEventType(eventType) {
      this.$confirm(`您确认删除活动类型 ${eventType.type} 吗?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          eventTypeApi.deleteEventTypeById(eventType.id).then(() => {
            this.$message({
              type: "success",
              message: "删除成功!",
            });
            this.getEventTypeList();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    saveEventType() {
      this.$refs.eventTypeFormRef.validate((valid) => {
        if (valid) {
          eventTypeApi.saveEventType(this.eventTypeForm).then(() => {
            this.$message({
              message: "保存成功",
              type: "success",
            });
            this.dialogFormVisible = false;
            this.getEventTypeList();
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    clearForm() {
      this.eventTypeForm = { type: "" ,description: ""};
      this.$refs.eventTypeFormRef.clearValidate();
    },
    cancel() {
      this.dialogFormVisible = false;
      this.clearForm();
    },
    openEditUI(id) {
      if (id == null) {
        this.title = "新增活动类型";
        this.eventTypeForm = {};
      } else {
        this.title = "修改活动类型";
        eventTypeApi.getEventTypeById(id).then((response) => {
          this.eventTypeForm = response.data;
        });
      }
      this.dialogFormVisible = true;
    },
    handleSizeChange(newSize) {
      this.searchModel.pageSize = newSize;
      this.searchModel.pageNo = 1;
      this.getEventTypeList();
    },
    handleCurrentChange(newPage) {
      this.searchModel.pageNo = newPage;
      this.getEventTypeList();
    },
    getEventTypeList() {
      eventTypeApi
        .getEventTypeList(this.searchModel)
        .then((response) => {
          console.log(response);
          this.total = response.data.total;
          this.eventTypeList = response.data.list;
        })
        .catch((error) => {
          console.error("获取活动类型列表失败", error);
        });
    },
  },
};
</script>

<style>
#search .el-input {
  width: 200px;
  margin-right: 10px;
}
</style>
